﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO; 
using System.Data;
using XmlAppliction.Doman; 
namespace XmlAppliction
{
    public class XMLHelper
    {

        public static void writeXMLTest( string path,string []names,string [] values)
        {
            XmlTextWriter xmltextwrite = new XmlTextWriter(path, Encoding.UTF8);
            xmltextwrite.Formatting = Formatting.Indented;
            xmltextwrite.WriteStartDocument();// start Document 
            xmltextwrite.WriteStartElement("NewDataSet");

            xmltextwrite.WriteStartElement("测试");
            for (int i = 0; i < names.Length;i++ )
            {


                xmltextwrite.WriteStartElement(names[i]);
                xmltextwrite.WriteString(values[i]);
                xmltextwrite.WriteEndElement();



            }
            xmltextwrite.WriteEndElement();

            xmltextwrite.WriteEndElement();
            xmltextwrite.WriteEndDocument();// end Document
            xmltextwrite.Flush();
            xmltextwrite.Close();
        
        }


        public static void writeXML(DataSet ds, string fold, string startTime, string endTime,string dotime, string  path)
        { 
            XmlTextWriter xmltextwrite = new XmlTextWriter(path, Encoding.UTF8);            
             xmltextwrite.Formatting= Formatting.Indented;
             xmltextwrite.WriteStartDocument();// start Document 
             xmltextwrite.WriteStartElement("NewDataSet");
             foreach (DataRow row in ds.Tables[0].Rows)
             {
                 xmltextwrite.WriteStartElement("统计数据表");


                 xmltextwrite.WriteStartElement("统计项名称");
                 xmltextwrite.WriteString(row["统计项名称"].ToString());
                 xmltextwrite.WriteEndElement();

                 xmltextwrite.WriteStartElement("统计项编号");
                 xmltextwrite.WriteString(row["ID"].ToString());
                 xmltextwrite.WriteEndElement(); 

                 xmltextwrite.WriteStartElement("类型");
                 xmltextwrite.WriteString(row["类型"].ToString());
                 xmltextwrite.WriteEndElement(); 

                 xmltextwrite.WriteStartElement("结果");
                 xmltextwrite.WriteString(row["结果"].ToString());
                 xmltextwrite.WriteEndElement();  

                 xmltextwrite.WriteEndElement();

             }
            xmltextwrite.WriteStartElement("统计任务基本信息表");
                xmltextwrite.WriteStartElement("统计数据开始时间");
                xmltextwrite.WriteString(startTime);
                xmltextwrite.WriteEndElement();  
                xmltextwrite.WriteStartElement("统计数据结束时间");
                xmltextwrite.WriteString(endTime);
                xmltextwrite.WriteEndElement(); 
                xmltextwrite.WriteStartElement("统计执行时间");
                xmltextwrite.WriteString(dotime);
                xmltextwrite.WriteEndElement(); 
                xmltextwrite.WriteStartElement("计算机名称");
                xmltextwrite.WriteString(System.Windows.Forms.SystemInformation.ComputerName);
                xmltextwrite.WriteEndElement(); 
            xmltextwrite.WriteEndElement();  


             xmltextwrite.WriteEndElement(); 
             xmltextwrite.WriteEndDocument();// end Document
             xmltextwrite.Flush();
             xmltextwrite.Close();
        
        }


        public static void writeFirstconfigXML (  string startTime, string endTime, string path)
        {
            try
            {
                XmlTextWriter xmltextwrite = new XmlTextWriter(path, Encoding.UTF8);
                xmltextwrite.Formatting = Formatting.Indented;
                xmltextwrite.WriteStartDocument();// start Document 
                xmltextwrite.WriteStartElement("config");

                xmltextwrite.WriteStartElement("baseSet");
                writeNoxeXML(xmltextwrite, "connectionString", "9F98D6AD44B8B3F9E1726B030C80F224944D03D069152C2143620E6B86115B47F9DFB04B2E9FE9580DF93476796FBB2F8BB56DFC6EFC79AF0D81AB460C3F081A6824ABD16B7A037822A1CC012C44C7DB");
                writeNoxeXML(xmltextwrite, "saveXMLPath", "");
                writeNoxeXML(xmltextwrite, "lastsaveXMLPath", "");
                writeNoxeXML(xmltextwrite, "wordMubanPath", "");
                writeNoxeXML(xmltextwrite, "saveWordPath", "");
                writeNoxeXML(xmltextwrite, "treeCondition", "");
                writeNoxeXML(xmltextwrite, "SSTJCondition", "   and  (统计项编号 like '07%' or 统计项编号 like '08%' or 统计项编号 like '09%') "); 
                writeNoxeXML(xmltextwrite, "统计项名称", "统计项名称");
                writeNoxeXML(xmltextwrite, "统计项编号", "统计项编号");
                writeNoxeXML(xmltextwrite, "执行语句", "执行语句");
                writeNoxeXML(xmltextwrite, "是否显示", "是否显示");
                writeNoxeXML(xmltextwrite, "执行状态", "执行状态");
                writeNoxeXML(xmltextwrite, "类型", "类型");
                writeNoxeXML(xmltextwrite, "结果", "结果");
                writeNoxeXML(xmltextwrite, "统计描述", "统计描述");
                writeNoxeXML(xmltextwrite, "连接参数编号", "连接参数编号");
                writeNoxeXML(xmltextwrite, "连接参数", "连接参数");
                xmltextwrite.WriteEndElement();


                xmltextwrite.WriteStartElement("统计任务基本信息表");
                xmltextwrite.WriteStartElement("统计数据开始时间");
                xmltextwrite.WriteString(startTime);
                xmltextwrite.WriteEndElement();
                xmltextwrite.WriteStartElement("统计数据结束时间");
                xmltextwrite.WriteString(endTime);
                xmltextwrite.WriteEndElement();
                xmltextwrite.WriteStartElement("统计执行时间");
                xmltextwrite.WriteString(DateTime.Now.ToString());
                xmltextwrite.WriteEndElement();
                xmltextwrite.WriteStartElement("计算机名称");
                xmltextwrite.WriteString(System.Windows.Forms.SystemInformation.ComputerName);
                xmltextwrite.WriteEndElement();
                xmltextwrite.WriteEndElement();


                xmltextwrite.WriteEndElement();
                xmltextwrite.WriteEndDocument();// end Document
                xmltextwrite.Flush();
                xmltextwrite.Close();
            }
            catch
            {
                throw new Exception("请用管理员身份运行程序！");
            }

        }
        static void writeNoxeXML(XmlTextWriter xmltextwrite, string fild, string value)
        {
            xmltextwrite.WriteStartElement(fild);
            xmltextwrite.WriteString(value);
            xmltextwrite.WriteEndElement();
        
        }


        public static void writeXMLChartConfig(string path, string titleFormateStr, List< ChartSerieUser> chartSerieUsers)
        {
            XmlTextWriter xmltextwrite = new XmlTextWriter(path, Encoding.UTF8);
            xmltextwrite.Formatting = Formatting.Indented;
            xmltextwrite.WriteStartDocument();// start Document 
            xmltextwrite.WriteStartElement("ChartConfig");

            xmltextwrite.WriteStartElement(titleFormateStr);
            foreach (ChartSerieUser user in chartSerieUsers)
            {
                xmltextwrite.WriteStartElement("Row");
                writeNoxeXML(xmltextwrite, "统计项名称", user.Name);
                writeNoxeXML(xmltextwrite, "统计项编号", user.TjId); 
                writeNoxeXML(xmltextwrite, "颜色","(255,0,0)");
                xmltextwrite.WriteEndElement();
            }
            xmltextwrite.WriteEndElement();

            xmltextwrite.WriteEndElement();
            xmltextwrite.WriteEndDocument();// end Document
            xmltextwrite.Flush();
            xmltextwrite.Close();

        }

        public static void writeXMLChartTable(string path, DataTable  dt)
        {
            XmlTextWriter xmltextwrite = new XmlTextWriter(path, Encoding.UTF8);
            xmltextwrite.Formatting = Formatting.Indented;
            xmltextwrite.WriteStartDocument();// start Document 
            xmltextwrite.WriteStartElement("Items");


            foreach (DataRow  row in dt.Rows)
            { 
                xmltextwrite.WriteStartElement("Item");
                writeNoxeXML(xmltextwrite, "名称", row["名称"].ToString()); 
                writeNoxeXML(xmltextwrite, "起始日期", row["起始日期"].ToString()); 
                writeNoxeXML(xmltextwrite, "结束日期", row["结束日期"].ToString()); 
                writeNoxeXML(xmltextwrite, "结果", row["结果"].ToString()); 
                xmltextwrite.WriteEndElement();
            }


            xmltextwrite.WriteEndElement();
            xmltextwrite.WriteEndDocument();// end Document
            xmltextwrite.Flush();
            xmltextwrite.Close();

        }

    }
}
